package com.prothink.controller.statistics;


import com.prothink.entity.ProductViews;
import com.prothink.service.ProductViewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *商品浏览量控制层
 * @Date 2022 11 24
 */
@CrossOrigin
@Controller
@RequestMapping("ProductViews")
public class ProductViewsController {
    @Autowired
    private ProductViewsService productViewsService;

    /**
     * 获取全部被浏览过的商品
     * @param productViews 商品浏览量实体类
     * @return 所有被搜索过的商品
     */
    @ResponseBody
    @RequestMapping("/getProductViews")
    public List getProductViews(ProductViews productViews){
        return productViewsService.getProductViews(productViews);
    }

    /**
     * 添加被浏览的商品信息
     * @param productViews 商品浏览量实体类
     * @return
     */
    @ResponseBody
    @RequestMapping("/addProductViews")
    public Integer addProductViews(ProductViews productViews){
        return productViewsService.addProductViews(productViews);
    }

    /**
     *
     * @param productViews 商品浏览量实体类
     * @return 近一个月或者近七天的数据
     */
    @ResponseBody
    @RequestMapping("/getProductViewsWeekandMod")
    public List getProductViewsWeekandMod(ProductViews productViews){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());
        try {
            Date parse = simpleDateFormat.parse(format);
            productViews.setBrowseTime(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return productViewsService.getProductWeekandModViews(productViews);
    }

    /**
     *
     * @param productViews 商品浏览量实体类
     * @return 上个月数据
     */
    @ResponseBody
    @RequestMapping("/getProductViewsOnMod")
    public List getProductViewsOnMod(ProductViews productViews){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());
        try {
            Date parse = simpleDateFormat.parse(format);
            productViews.setBrowseTime(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return productViewsService.getProductOnModViews(productViews);
    }
    /**
     *
     * @param productViews 商品浏览量实体类
     * @return 上周数据
     */
    @ResponseBody
    @RequestMapping("/getProductViewsOnWeek")
    public List getProductViewsOnWeek(ProductViews productViews){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());
        try {
            Date parse = simpleDateFormat.parse(format);
            productViews.setBrowseTime(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return productViewsService.getProductOnWeek(productViews);
    }
}
